#! /bin/sh

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin


PWD_dir=`pwd`
SRC_parentdir=/usr/local/src
SRC_tgz=git-2.0.5.tar.gz
SRC_dir=`basename $SRC_tgz .tar.gz`
SRC_name=git

DEST_dir=/tmp/$SRC_dir

RPM_version='2.0.5.dt2.el6'

[ -d $DEST_dir ] || mkdir -p $DEST_dir


Packages="make gcc libcurl-devel expat-devel gettext-devel \
 openssl-devel zlib-devel asciidoc xmlto perl-ExtUtils-MakeMaker \
 rubygems ruby-devel rpm-build"
for package in $Packages; do
    rpm -q $package || yum -y install $package

done


if [ ! -x /usr/bin/db2x_docbook2texi ]; then
    rpm -q epel-release || yum -y install http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
    yum -y install docbook2X
    ln -s /usr/bin/db2x_docbook2texi /usr/bin/docbook2x-texi
else
    echo "docbook2X have installed"
fi


cd $SRC_parentdir
# if [ -f $SRC_tgz ]; then
if [ ! -f $SRC_tgz ]; then
    curl -LSso $SRC_tgz https://www.kernel.org/pub/software/scm/git/$SRC_tgz
    tar zxf $SRC_tgz
    cd $SRC_dir
    make prefix=/usr all doc info
    make prefix=/usr install install-doc install-html install-info DESTDIR=$DEST_dir
fi


# install fpm
if [ ! -x /usr/bin/fpm ]; then
    echo "fpm were not installed"
    gem install fpm --source https://ruby.taobao.org/
else
    echo "fpm have installed"
fi
#######
cd $PWD_dir

fpm -s dir -t rpm -v $RPM_version --epoch 2 \
    -n $SRC_name \
    -C $DEST_dir .


#rm -rf $DEST_dir
##
